package com.chen.spring.cloud.service.good.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.entity.GTicketsPrice;
import com.chen.entity.Goods;
import com.chen.entity.GoodsType;
import com.chen.spring.cloud.service.good.service.IGTicketsPriceService;
import com.chen.spring.cloud.service.good.service.IGoodService;
import com.chen.spring.cloud.service.good.service.IGoodsTypeService;
import com.chen.vo.GoodListVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/good")
public class GoodController {

    @Autowired
    private IGoodService goodService;

    @Autowired
    private IGoodsTypeService goodsTypeService;

    @Autowired
    private IGTicketsPriceService ticketsPriceService;


    @RequestMapping("/list")
    public Map<String,Object> list(@RequestParam(value = "gt_id") String gt_id,
                     @RequestParam(value = "hot_tab") Integer hot_tab,
                     @RequestParam(value = "limit") String limit,
                     @RequestParam(value = "is_recommend") String is_recommend,
                     @RequestParam(value = "is_hot") String is_hot) {

        Map<String,Object> result = new HashMap<>();
        Page<Goods> page = new Page<>();
        if (!StringUtils.isEmpty(limit)){
            String[] pageInfo = limit.split(",");
            page.setCurrent(Integer.parseInt(pageInfo[0]));
            page.setSize(Integer.parseInt(pageInfo[1]));
        }else {
            page.setCurrent(1);
            page.setSize(15);
        }
        QueryWrapper<Goods> wrapper = new QueryWrapper<Goods>().eq("status",1);

        if (!StringUtils.isEmpty(is_recommend)){
            wrapper.eq("show_label","推荐");
        }

        if (!StringUtils.isEmpty(gt_id)){
            wrapper.eq("show_tab",gt_id);
        }

        if (!StringUtils.isEmpty(is_hot)){
            wrapper.eq("show_label","热门").or().eq("show_tab",99);
        }

        wrapper.orderByDesc("sort","goods_id");

        Page<Goods> data = (Page<Goods>)goodService.page(page,wrapper);

        List<GoodListVo> listVos = new ArrayList<>();
        for (Goods goods:data.getRecords()) {
            GoodListVo goodListVo = new GoodListVo();
            BeanUtil.copyProperties(goods,goodListVo);
            listVos.add(goodListVo);
        }
        result.put("totalcount",data.getTotal());
        result.put("list",listVos);
        return result;
    }

    @RequestMapping("/details")
    public Map details(Integer goods_id){
        Goods goods = goodService.getOne(new QueryWrapper<Goods>().eq("goods_id",goods_id));

        //门票存在阶梯票价
        if(goods.getGt_id() == 2){
            GTicketsPrice gTicketsPrice = ticketsPriceService.getOne(new QueryWrapper<GTicketsPrice>().eq("goods_id",goods.getGoods_id()));
            goods.setPrice(gTicketsPrice.getPrice());
            goods.setOri_price(gTicketsPrice.getOri_price());
            goods.setDiscount_amount(gTicketsPrice.getDiscount_amount());
        }


        if (StringUtils.isEmpty(goods.getAssist_start_time()) && StringUtils.isEmpty(goods.getAssist_end_time()) && StringUtils.isEmpty(goods.getAssist_msg())){
            goods.setAssist_msg(goods.getAssist_start_time() + " - " +goods.getAssist_end_time());
        }

        GoodsType goodsType = goodsTypeService.getOne(new QueryWrapper<GoodsType>().eq("gt_id",goods.getGt_id()));
        String tname = "zs_"+goodsType.getTbl_name();
        Map<String,Object> attr = goodService.getAttr(tname,goods_id);

        switch (goods.getGt_id()){
            case 1:
                Map<String,Object> Luxian = buildLuxian(goods,attr);
                return Luxian;
            case 2:
                Map<String,Object> Tickets = buildTickets(goods,attr);
                return Tickets;
            case 3:
                try {
                    return BeanUtils.describe(goods);
                } catch (Exception e){
                    e.printStackTrace();
                }
                return null;
            case 4:
                Map<String,Object> combo = buildCombo(goods,attr);
                return combo;
            case 5:
                Map<String,Object> ty = buildLuxian(goods,attr);
                return ty;
            default:
                return buildOther(goods,attr);
        }

    }

    public Map<String,Object> buildLuxian(Goods goods,Map<String,Object> attr){
        Map<String,Object> result = new HashMap<>();
        result.put("goods_id",goods.getGoods_id());
        result.put("gt_id",goods.getGt_id());
        result.put("goods_title",goods.getGoods_title());
        result.put("goods_info",goods.getGoods_info());
        result.put("goods_img",goods.getGoods_img());
        result.put("goods_big_img",goods.getGoods_big_img());
        result.put("price",goods.getPrice());
        result.put("discount_amount",goods.getDiscount_amount());
        result.put("ori_price",goods.getOri_price());
        result.put("show_label",goods.getShow_label());
        result.put("ticket_limit",goods.getTicket_limit());
        result.put("assist_msg",goods.getAssist_msg());
        result.put("recommend_reason",goods.getRecommend_reason());
        result.put("ticket_category",goods.getTicket_category());
        result.put("min",attr.get("min"));
        result.put("max",attr.get("max"));
        result.put("is_group",attr.get("is_group"));
        result.put("fee_purchase_note",attr.get("fee_purchase_note"));
        result.put("fee_include",attr.get("fee_include"));
        result.put("fee_exclude",attr.get("fee_exclude"));
        result.put("tip_refund_policy",attr.get("tip_refund_policy"));
        result.put("tip_import_note",attr.get("tip_import_note"));
        result.put("tip_service_msg",attr.get("tip_service_msg"));
        result.put("report_cut_time",attr.get("report_cut_time"));

        return result;
    }

    public Map<String,Object> buildTickets(Goods goods,Map<String,Object> attr){
        Map<String,Object> result = new HashMap<>();
        result.put("goods_id",goods.getGoods_id());
        result.put("gt_id",goods.getGt_id());
        result.put("goods_title",goods.getGoods_title());
        result.put("goods_info",goods.getGoods_info());
        result.put("goods_img",goods.getGoods_img());
        result.put("goods_big_img",goods.getGoods_big_img());
        result.put("price",goods.getPrice());
        result.put("discount_amount",goods.getDiscount_amount());
        result.put("ori_price",goods.getOri_price());
        result.put("show_label",goods.getShow_label());
        result.put("ticket_limit",goods.getTicket_limit());
        result.put("assist_msg",goods.getAssist_msg());
        result.put("buy_cut_time",goods.getBuy_cut_time());
        result.put("recommend_reason",goods.getRecommend_reason());
        result.put("ticket_category",goods.getTicket_category());
        result.put("fee_purchase_note",attr.get("fee_purchase_note"));
        result.put("fee_include",attr.get("fee_include"));
        result.put("fee_exclude",attr.get("fee_exclude"));
        result.put("tip_refund_policy",attr.get("tip_refund_policy"));
        result.put("tip_import_note",attr.get("tip_import_note"));
        result.put("tip_service_msg",attr.get("tip_service_msg"));
        result.put("is_appointment",goods.getIs_appointment());

        return result;
    }

    public Map<String,Object> buildCombo(Goods goods,Map<String,Object> attr){
        Map<String,Object> result = new HashMap<>();
        result.put("goods_id",goods.getGoods_id());
        result.put("gt_id",goods.getGt_id());
        result.put("goods_title",goods.getGoods_title());
        result.put("goods_info",goods.getGoods_info());
        result.put("goods_img",goods.getGoods_img());
        result.put("goods_big_img",goods.getGoods_big_img());
        result.put("price",goods.getPrice());
        result.put("discount_amount",goods.getDiscount_amount());
        result.put("ori_price",goods.getOri_price());
        result.put("show_label",goods.getShow_label());
        result.put("ticket_limit",goods.getTicket_limit());
        result.put("assist_msg",goods.getAssist_msg());
        result.put("buy_cut_time",goods.getBuy_cut_time());
        result.put("recommend_reason",goods.getRecommend_reason());
        result.put("ticket_category",goods.getTicket_category());
        result.put("fee_purchase_note",attr.get("fee_purchase_note"));
        result.put("fee_include",attr.get("fee_include"));
        result.put("fee_exclude",attr.get("fee_exclude"));
        result.put("tip_import_note",attr.get("tip_import_note"));
        result.put("tip_service_msg",attr.get("tip_service_msg"));
        result.put("tip_refund_policy",attr.get("tip_refund_policy"));
        return result;
    }

    public Map<String,Object> buildOther(Goods goods,Map<String,Object> attr){
        Map<String,Object> result = new HashMap<>();
        result.put("goods_id",goods.getGoods_id());
        result.put("gt_id",goods.getGt_id());
        result.put("goods_title",goods.getGoods_title());
        result.put("goods_info",goods.getGoods_info());
        result.put("goods_img",goods.getGoods_img());
        result.put("goods_big_img",goods.getGoods_big_img());
        result.put("price",goods.getPrice());
        result.put("discount_amount",goods.getDiscount_amount());
        result.put("ori_price",goods.getOri_price());
        result.put("show_label",goods.getShow_label());
        result.put("ticket_limit",goods.getTicket_limit());
        result.put("assist_msg",goods.getAssist_msg());
        result.put("buy_cut_time",goods.getBuy_cut_time());
        result.put("recommend_reason",goods.getRecommend_reason());
        result.put("ticket_category",goods.getTicket_category());
        return result;
    }

}
